package com.cqvip.innocence.framework.security;

import com.alibaba.fastjson.JSON;
import com.cqvip.innocence.framework.jwt.dto.LoginAdmin;
import com.cqvip.innocence.framework.jwt.service.AdminTokenService;
import com.cqvip.innocence.project.model.dto.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName LogoutSuccessHandlerImpl
 * @Description: 自定义退出处理类 返回成功
 * @Author Innocence
 * @Date 2022/5/26 15:37
 * @Version 1.0
 */
@Configuration
public class LogoutSuccessHandlerImpl implements LogoutSuccessHandler {

    @Autowired
    private AdminTokenService tokenService;

    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        LoginAdmin loginAdmin = tokenService.getLoginAdmin(tokenService.getTokenFromRequest(request));
        if (null != loginAdmin) {
            tokenService.delLoginAdmin(tokenService.getTokenFromRequest(request));
        }
        response.setStatus(200);
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        response.getWriter().print(JSON.toJSONString(JsonResult.Get(true, "退出成功！")));
    }
}
